/**返回使 s 变成 交替字符串 所需的最少操作次数
 * @param {string} s
 * @return {number}
 * 只能变成010101...或101010...
 */
var minOperations = function (s) {
    let count = 0

    // 替换字符串的指定下标位
    function replaceWord(str, index, newWords) {
        let arr = str.split('')
        arr[index] = newWords
        return arr.join('')
    }

    for (let i = 1; i < s.length; i++) {
        if (s[i] == s[i - 1]) {
            count++
            if (s[i] == '0') {
                s = replaceWord(s, i, '1')
            } else {
                s = replaceWord(s, i, '0')
            }
        }
    }

    // 若变成010101...的操作次数是count，则变成101010...的操作次数是s.length - count
    let changeFisrtCount = s.length - count

    return Math.min(count, changeFisrtCount)
};


var minOperations = function (s) {
    let cnt = 0;
    // 计算变成010101...的操作次数
    for (let i = 0; i < s.length; i++) {
        const c = s[i];
        if (c !== (String.fromCharCode('0'.charCodeAt() + i % 2))) {
            cnt++;
        }
    }
    return Math.min(cnt, s.length - cnt);
};